package com.itheima.server.controller;


import com.alibaba.excel.EasyExcel;
import com.itheima.common.core.domain.AjaxResult;

import com.itheima.server.domain.Clue;
import com.itheima.server.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class MyTestController{

    @Autowired
    ClueService clueService;
    //测试用，可删除
    @GetMapping
    public AjaxResult test(){
        return new AjaxResult(200,"成功");
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, Clue clue) throws IOException, IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''clue.xlsx");

        List<Clue> list = new ArrayList<>(); // 或者根据条件查询: clueService.pageQuery(dto)

        EasyExcel.write(response.getOutputStream(), Clue.class)
                .sheet("线索数据")
                .doWrite(list);
    }

}
